package com.bwie.controller;

import com.alibaba.fastjson.JSONObject;
import com.bwie.pojo.Bargaining;
import com.bwie.pojo.GoodsOrder;
import com.bwie.pojo.Order;
import com.bwie.pojo.User;
import com.bwie.service.BargainingService;
import com.bwie.service.OrderService;
import com.bwie.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private BargainingService bargainingService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private RecordService recordService;


    //添加订单信息，砍价订单的创建
    @RequestMapping("/addOrder")
    @ResponseBody
    public int addOrder(Integer bargainingId){

        String s = redisTemplate.opsForValue().get("user");
        User user = JSONObject.parseObject(s, User.class);

        Order order1 = orderService.getOrder(user.getUserId());
        //判断当前订单列表是否存在当前用户以及下过的订单
        if (order1!=null){
            return 2;
        }

        Bargaining bargaining = bargainingService.echo(bargainingId);
        //添加订单表
        Order order = new Order();
        order.setBargainingId(bargainingId);
        order.setGuize(bargaining.getGuize());
        order.setKnife(bargaining.getKnife());
        order.setMoney(bargaining.getMoney());
        order.setPrice(bargaining.getGoodsPrice());
        order.setUserId(user.getUserId());
        orderService.addOrder(order);

        //订单表添加完后立即砍自己一刀
        //添加一条记录到信息表
        //如果规则为1
        if (order.getGuize()==1){
            //商品原价
            Double goodsPrice = bargaining.getGoodsPrice();
            //商品底价
            Double floorPrice = bargaining.getFloorPrice();
            //获取共砍多少刀砍至底价
            Integer knife = order.getKnife();
            //平均每刀砍多少元
            Double n = (goodsPrice-floorPrice)/knife;
            //用户创建订单后给自己砍一刀然后赋值给当前价格
            order.setPrice(order.getPrice()-n);
            //重新修改这个对象
            int i = orderService.updateOrder(order);
            if (i>0){
                //修改成功后添加一条信息到记录表
                recordService.addRecord(order.getOrderId(),user.getUserId());
            }
            return i;
        }
        //如果规则为2
        if (order.getGuize()==2){
            //商品原价
            Double goodsPrice = bargaining.getGoodsPrice();
            //商品底价
            Double floorPrice = bargaining.getFloorPrice();
            //获取每刀砍多少元
            Double money = order.getMoney();
            int round = (int) Math.round(money);
            int asInt = new Random().ints(1, round).findAny().getAsInt();
            //减去一次砍掉的价格
            Double n = order.getPrice()-asInt;
            //如果小于底价则将当前价格改为底价
            if (n<floorPrice){
                n = floorPrice;
            }
            //将砍掉的价格赋值给order对象
            order.setPrice(n);
            //修改
            int i = orderService.updateOrder(order);
            if (i>0){
                //修改成功后添加一条信息到记录表
                recordService.addRecord(order.getOrderId(),user.getUserId());
            }
            return i;
        }
        return 0;
    }

    //前往用户自己的砍价详情
    @RequestMapping("/goOrder")
    public String goOrder(){
        return "order";
    }

    //砍价详情界面根据用户id回显
    @RequestMapping("/echo")
    @ResponseBody
    public GoodsOrder echo(){
        String s = redisTemplate.opsForValue().get("user");
        User user = JSONObject.parseObject(s, User.class);
        return orderService.getGoodsOrder(user.getUserId());
    }

    //砍价详情界面根据订单id回显
    @RequestMapping("/echo2")
    @ResponseBody
    public GoodsOrder echo2(Integer orderId){
        return orderService.echo2(orderId);
    }

    //帮好友砍一刀
    @RequestMapping("/bargain")
    @ResponseBody
    public int bargain(Integer orderId, Integer userId){
        String s = redisTemplate.opsForValue().get("user");
        User user = JSONObject.parseObject(s, User.class);
        if (userId.equals(user.getUserId())){
            return 0;
        }

        //判断记录表中是否有当前登录用户给这个商品砍过价
        int i = recordService.selectRecord(orderId,user.getUserId());
        if (i>0){
            return 2;
        }

        //获取订单对象
        Order order = orderService.getOrder2(orderId);

        Bargaining bargaining = bargainingService.echo(order.getBargainingId());
        //添加一条记录到信息表
        //如果规则为1
        if (order.getGuize()==1){
            //商品原价
            Double goodsPrice = bargaining.getGoodsPrice();
            //商品底价
            Double floorPrice = bargaining.getFloorPrice();
            //获取共砍多少刀砍至底价
            Integer knife = order.getKnife();
            //平均每刀砍多少元
            Double n = (goodsPrice-floorPrice)/knife;
            //砍一刀，减去平均每刀砍的价钱
            double v = order.getPrice() - n;
            if (v<floorPrice){
                v = floorPrice;
            }
            order.setPrice(v);
            //重新修改这个对象
            int i2 = orderService.updateOrder(order);
            if (i2>0){
                //修改成功后添加一条信息到记录表
                recordService.addRecord(order.getOrderId(),user.getUserId());
            }
            return i2;
        }

        //如果规则为2
        if (order.getGuize()==2){
            //商品原价
            Double goodsPrice = bargaining.getGoodsPrice();
            //商品底价
            Double floorPrice = bargaining.getFloorPrice();
            //获取每刀砍多少元
            Double money = order.getMoney();
            int round = (int) Math.round(money);
            int asDouble = new Random().ints(1, round).findAny().getAsInt();
            System.out.println("您的好友帮您砍了"+asDouble+"元");
            //减去一次砍掉的价格
            Double n = order.getPrice()-asDouble;
            //如果小于底价则将当前价格改为底价
            if (n<floorPrice){
                n = floorPrice;
            }
            //将砍掉的价格赋值给order对象
            order.setPrice(n);
            //修改
            int i1 = orderService.updateOrder(order);
            if (i1>0){
                //修改成功后添加一条信息到记录表
                recordService.addRecord(order.getOrderId(),user.getUserId());
            }
            return i1;
        }
        return 3;
    }


    //查询订单列表中是否有该用户选择的砍价商品
    @RequestMapping("/findOrder")
    @ResponseBody
    public int findOrder(){
        User user = JSONObject.parseObject(redisTemplate.opsForValue().get("user"), User.class);
        Order order = orderService.getOrder(user.getUserId());
        if (order==null){
            return 0;
        }else {
            return 1;
        }

    }

}
